<?php
declare (strict_types = 1);

namespace app\admin\model;

use think\Exception;
use think\facade\Config;
use think\Model;

/**
 * @mixin \think\Model
 */
class brand extends Model
{
    //
    protected $pk = 'brand_id';
    //模糊搜索
    protected $error;
    public function getError(){
        return $this->error;
    }
    public function setPvaluesAttr($value){
        if (is_array($value)){
            return implode(',',$value);
        }
        return $value;
    }
    public function searchBrandNameAttr($query,$value,$data){
        if ('' !== $value){
            $query->whereLike('brand_name','%' . $value . '%');
        }
    }
    public function searchRecommendedAttr($query,$value,$data){
        //针对复选框形式
        if (is_array($value) || (is_scalar($value) && strpos('#' . $value,','))){
            $query->whereIn('ecm_brand.recommended',intval($value));
        }
        //针对单选框形式
        if (is_scalar($value) && false === strpos($value,',') && intval($value) < 2){
            $query->where('ecm_brand.recommended',intval($value));
        }
    }
    public function searchIfShowAttr($query,$value){
        if (is_array($value) || (is_scalar($value) && strpos('#'.$value,','))){
            $query->whereIn('ecm_brand.if_show',$value);
        }
    }
    //有图无图
    public function searchBrandLogoAttr($query,$value,$data){
        if (is_array($value) && 1==count($value) && 1 == $value[0]){
            $query->where(function ($q){
                $q->where('brand_logo','<>','')->where('brand_logo','<>','Null');
            });
        }
        if (is_array($value) && 1==count($value) && 0 == $value[0]){
            $query->where(function ($q){
                $q->where('brand_logo','')->whereOr('brand_logo','Null');
            });
        }
//        if(isset($value) && 1 == count($value) && 1 == $value[0]){
//            $query->where('brand_logo','<>','Null');
//        }

//        if(isset($value) && 1 == count($value) &&  $value[0] ==0){
//            $query->where('brand_logo','Null');
//        }
    }
    public function getRecommendedZhAttr($value,$data){
        $ret = [0 => '不推荐', 1=>'推荐'];
        return $ret[$data['recommended']];
    }
    public function getIfShowZhAttr($value,$data){
        $ret = [0 =>'无效', 1=>'有效'];
        return $ret[$data['if_show']];
    }
    public function searchPageAttr($query, $value)
    {
    }
    public function GetBrandLogoAttr($value){
        return $value==''?'default.png':$value;
    }

    public function add(array $data,array $allowField = [],$replace = false){
        try{
            if (empty($data)){
                $this->error='数据不为空';
            }
            $this->save($data);
            $id=$this->toArray();
            return $id[$this->pk];
        }catch (\Exception $e){
            $this->error = $e->getMessage();
            return false;
        }
    }

    public static function getPinList($data = [],$query = [],$page = false){
        $model = self::field(true)->order('sort_order')
        ->withSearch(array_keys($data),$data);
//        if (isset($data['brand_name']) && '' !== $data['brand_name']){
//            $model->whereLike('brand_name','%'.$data['brand_name'].'%');
//        }
//
//        if (isset($data['recommended']) && '2'!=$data['recommended']){
//            $model->whereIn('recommended',$data['recommended']);
//        }
//        if(isset($data['brand_logo']) && 1 == count($data['brand_logo']) && 1 == $data['brand_logo'][0]){
//            $model->where('brand_logo','<>','Null');
//        }
//        if(isset($data['brand_logo']) && 1 == count($data['brand_logo']) &&  $data['brand_logo'][0] ==0){
//            $model->where('brand_logo','Null');
//        }

        $list_rows = Config::has('view.list_rows')?Config::get('view.list_rows'):15;

        if ($page){
            $result = $model->select();
        }else{
            $result = $model->paginate([
                'list_rows' => $list_rows,
                'query'     => $query,
            ]);
        }
        return $result;

    }
    public static function BrandList(){
        $model = self::field('brand_id,brand_name')->select();
        return $model;
    }
    public static function onBeforeDelete($brand)
    {
        if (goods::checkBrandId($brand->brand_id)){
            throw new Exception("品牌在商品中已经使用！无法删除");
        }
    }
    public static function delbrand($id){
        self::startTrans();
        try{
            if (is_string($id) && false === strpos($id,',')){
                $brandObj = self::find($id);
                if ($brandObj && $brandObj->delete()){
                    self::commit();
                    return true;
                }
                return false;
            }
            if (is_array($id) || (is_string($id) && false !== strpos($id,','))){
                $resultSet = self::whereIn('brand',$id)->select();
                foreach ($resultSet as $result){
                    $ret = $result->force(true)->delete();
                    if ($ret === false){
                        self::rollback();
                        return false;
                    }
                }
                self::commit();
                return true;
            }
            return;
        }catch (\Exception $e){
            self::rollback();
            return false;
        }
    }
    public function upt($data, $where = [],$allowField = []){
        try{
            if (empty($data)){
                $this->error = "修改数据不能为空";
                return ;
            }

            if (!empty($allowField)) {
                $this->allowField($allowField);
            }
            if (!empty($where)){
                $this->setUpdateWhere($where);

                if (isset($data[$this->pk]))   unset($data[$this->pk]);
            }

            return $this->exists(true)->save($data);
        }catch (Exception $e){
            $this->error = $e->getMessage();
            return false;
        }
    }
}
